﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using Model;
using DataServices;

namespace LFWeb3.Controllers
{
    public class ArtWorkController : Controller
    {
        DaoArtGroup ArtGroupDao = new DaoArtGroup();
        DaoArtWork ArtWorkDao = new DaoArtWork();
        DaoArtist artistDao = new DaoArtist();

        public ActionResult ArtWorkGroup()
        {
            ViewBag.ArtWorkGroup = ArtGroupDao.LoadAllArtGroup();
            return View();
        }

        public ActionResult ArtWork(string artist)
        {
            //最新拍卖作品
            ViewBag.NewArtWork = ArtWorkDao.LoadTopArtWork();
            //艺术家
            ViewBag.Artist = artistDao.LoadAllArtists();
            //作品图片
            if (artist != null)
            {
                ViewBag.ArtWorkPic = ArtWorkDao.LoadArtWorkPic(artist);
            }
            else
            {
                ViewBag.ArtWorkPic = ArtWorkDao.LoadArtWorkPic();
            }
            return View();
        }

        public ActionResult ViewArtWork(string id)
        {
            ViewBag.ArtWork = ArtWorkDao.LoadArtworkByID(id);
            return View();
        }

        /// <summary>
        /// 添加艺术品
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            ViewBag.Artist = artistDao.LoadAllArtists();
            ViewBag.ArtGroup = ArtGroupDao.LoadAllArtGroup();
            return View();
        }

        public ActionResult Edit(string id)
        {
            ViewBag.ArtWork = ArtWorkDao.LoadAllArtwork();
            return View();
        }

        public ActionResult Update(string id)
        {
            ViewBag.ArtWork = ArtWorkDao.LoadArtworkByID(id);
            ViewBag.Artist = artistDao.LoadAllArtists();
            ViewBag.ArtGroup = ArtGroupDao.LoadAllArtGroup();
            return View();
        }

        /// <summary>
        /// 新增艺术品
        /// </summary>
        /// <param name="artWork"></param>
        /// <returns></returns>
        [ValidateInput(false)]
        public JsonResult AddArtWork(ArtWorkInfo artWork)
        {
            try
            {
                artWork.ID = Guid.NewGuid().ToString();
                if (artWork.SaleTime == null)
                {
                    artWork.SaleTime = "";
                }
                if (artWork.SaleCompany == null)
                {
                    artWork.SaleCompany = "";
                }
                if (artWork.SaleAuct == null)
                {
                    artWork.SaleAuct = "";
                }
                ArtWorkDao.AddArtWork(artWork);
                return Json("success", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }

        /// <summary>
        /// 更新艺术品信息
        /// </summary>
        /// <param name="artWork"></param>
        /// <returns></returns>
        [ValidateInput(false)]
        public JsonResult updateArtWork(ArtWorkInfo artWork)
        {
            try
            {
                if (artWork.SaleTime == null)
                {
                    artWork.SaleTime = "";
                }
                if (artWork.SaleCompany == null)
                {
                    artWork.SaleCompany = "";
                }
                if (artWork.SaleAuct == null)
                {
                    artWork.SaleAuct = "";
                }
                ArtWorkDao.UpdateArtWork(artWork);
                return Json("success", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }

        [ValidateInput(false)]
        public JsonResult AddArtWorkGroup(string ArtWorkGroupName)
        {
            try
            {
                ArtGroupDao.AddArtGroup(ArtWorkGroupName);
                return Json("success", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }

        [ValidateInput(false)]
        public JsonResult DeleteArtWorkGroup(string ArtWorkGroupName)
        {
            try
            {
                ArtGroupDao.DeleteArtGroup(ArtWorkGroupName);
                return Json("success", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }

        /// <summary>
        /// 删除艺术品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [ValidateInput(false)]
        public JsonResult DeleteArtWork(string id)
        {
            try
            {
                this.ArtWorkDao.DeleteArtWork(id);
                return Json("success", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }
    }
}